using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using InfoSoftGlobal;

public partial class ComparisionChart : System.Web.UI.Page
{
    SQLInteract objSQL = new SQLInteract();

    protected void Page_Load(object sender, EventArgs e)
    {
        fillSystemdropdown();
    }

    protected void ddSystem_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddRelease.Items.Clear();
        ListItem li = new ListItem("Choose");
        li.Value = "-1";
        ddRelease.Items.Add(li);
        btnChart.Visible = false;
        fcLiteral.Visible = false;
        fillReleasedropdown();
    }

    public void fillSystemdropdown()
    {
        DataSet dsSystem = new DataSet();
        try
        {
            dsSystem = objSQL.ReturnDataAdapter("select distinct System_Detected_In from Defects order by System_Detected_In desc");
            ddSystem.DataSource = dsSystem;
            ddSystem.DataTextField = "System_Detected_In";
            ddSystem.DataValueField = "System_Detected_In";
            ddSystem.DataBind();
        }
        catch (Exception ex)
        {
            this.ClientScript.RegisterStartupScript(this.Page.GetType(), "Alert", "<script language=\"javascript\" type=\"text/javascript\" >alert(\"Error: " + ex.Message + "\");</script>", false);
        }
        finally
        {
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        fcLiteral.Visible = false;
        btnChart.Visible = true;
    }

    public void fillReleasedropdown()
    {
        DataSet dsRelease = new DataSet();
        try
        {
            dsRelease = objSQL.ReturnDataAdapter("select distinct Detected_In_Release from Defects where System_Detected_In='" + ddSystem.SelectedValue.ToString().Trim() + "' order by Detected_In_Release desc");
            ddRelease.DataSource = dsRelease;
            ddRelease.DataTextField = "Detected_In_Release";
            ddRelease.DataValueField = "Detected_In_Release";
            ddRelease.DataBind();
        }
        catch (Exception ex)
        {
            this.ClientScript.RegisterStartupScript(this.Page.GetType(), "Alert", "<script language=\"javascript\" type=\"text/javascript\" >alert(\"Error: " + ex.Message + "\");</script>", false);
        }
        finally
        {
        }
    }

    protected void btnChart_Click(object sender, EventArgs e)
    {
        string release_name = ddRelease.SelectedValue.ToString().Trim();
        SqlDataReader sdr, sdr1 = null;
        Array statusSTarray;
        Array statusUATarray;
        Array statusSITarray;
        ArrayList arrDates = new ArrayList();

        int statusST = 0;
        string statusSTList = "";

        int statusUAT = 0;
        string statusUATList = "";

        int statusSIT = 0;
        string statusSITList = "";

        sdr = objSQL.ReturnDataReader("select distinct(Detected_on_Date_From) from Defects where System_Detected_In='" + ddSystem.SelectedValue.ToString().Trim() + "' and Detected_In_Release like '%" + release_name + "%' order by Detected_on_Date_From");
        if (sdr.HasRows)
        {
            while (sdr.Read())
            {
                arrDates.Add(DateTime.Parse(sdr["Detected_on_Date_From"].ToString()).ToString("MM/dd/yyyy"));
            }
        }

        foreach (string item in arrDates)
        {
            if (item != "")
            {
                statusST = 0;
                statusUAT = 0;
                statusSIT = 0;
                sdr1 = objSQL.ReturnDataReader("select * from Defects where System_Detected_In='" + ddSystem.SelectedValue.ToString().Trim() + "' and Detected_on_Date_From <= '" + item + "' and  Detected_In_Release like '%" + release_name + "%' order by Detected_on_Date_From");
                if (sdr1.HasRows)
                {
                    while (sdr1.Read())
                    {
                        if (sdr1["Detected_In_Cycle"].ToString().Trim() == "d - System Test")
                        {
                            statusST = statusST + 1;
                        }
                        else if (sdr1["Detected_In_Cycle"].ToString().Trim() == "g - UAT")
                        {
                            statusUAT = statusUAT + 1;
                        }
                        else if (sdr1["Detected_In_Cycle"].ToString().Trim() == "e - SIT")
                        {
                            statusSIT = statusSIT + 1;
                        }
                    }
                    statusSTList = statusSTList + statusST.ToString().Trim() + ",";
                    statusUATList = statusUATList + statusUAT.ToString().Trim() + ",";
                    statusSITList = statusSITList + statusSIT.ToString().Trim() + ",";
                }
            }
        }
        statusSTarray = statusSTList.Split(new char[] { ',' });
        statusUATarray = statusUATList.Split(new char[] { ',' });
        statusSITarray = statusSITList.Split(new char[] { ',' });

        string strXML1 = GenerateXML(arrDates, statusSTarray, statusUATarray, statusSITarray, release_name);
        fcLiteral.Text = FusionCharts.RenderChart("FusionCharts/FCF_MSLine.swf", "", strXML1, "myNext", "800", "500", false, false);
        fcLiteral.Visible = true;
    }

    public string GenerateXML(ArrayList date_list, Array statusSTarray, Array statusUATarray, Array statusSITarray, string release_name)
    {
        string strXML = "";

        strXML = "<graph caption='Defect Status' subcaption='' hovercapbg='FFECAA' hovercapborder='F47E00' formatNumberScale='0' decimalPrecision='0' showvalues='0' numdivlines='10' numVdivlines='0' yaxisminvalue='100' yaxismaxvalue='100' rotateNames='1'>";

        //CATEGORIES
        strXML += "<categories>";
        foreach (string items in date_list)
        {
            if (items.ToString() != "")
            {
                strXML += "<category name='" + items.ToString().Trim() + "' />";
            }
        }
        strXML += "</categories>";

        //System test
        strXML += "<dataset seriesName='System Testing' color='F1683C' anchorBorderColor='1D8BD1' anchorBgColor='1D8BD1'>";
        foreach (string items in statusSTarray)
        {
            if (items != "")
            {
                strXML += "<set value='" + items + "' />";
            }
        }
        strXML += "</dataset>";

        //UAT
        strXML += "<dataset seriesName='UAT' color='2AD62A' anchorBorderColor='1D8BD1' anchorBgColor='1D8BD1'>";
        foreach (string item in statusUATarray)
        {
            if (item != "")
            {
                strXML += "<set value='" + item + "' />";
            }
        }
        strXML += "</dataset>";

        //SIT
        strXML += "<dataset seriesName='SIT' color='888888' anchorBorderColor='1D8BD1' anchorBgColor='1D8BD1'>";
        foreach (string item in statusSITarray)
        {
            if (item != "")
            {
                strXML += "<set value='" + item + "' />";
            }
        }
        strXML += "</dataset>";
        strXML += "</graph>";
        return strXML;
    }
}
